package com.are.odisea;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class SrvMenu
 */
@WebServlet("/SrvMenu")
public class SrvMenu extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SrvMenu() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void ProcesarPeticion(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
    	response.setContentType("text/html;charset=ISO-8859-1");
    	PrintWriter out = response.getWriter();

		String operacion = (String) request.getParameter("operacion");
		if(operacion.equals("listar")){
			LlenarMenu(request,out);
		}		
    }
    
    protected void LlenarMenu(HttpServletRequest request, PrintWriter out){
		try {
			db conexion = null;
	    	conexion = new db();
			ManejadorMenu MN = new ManejadorMenu(conexion);
			ArrayList<Menu> menuP = MN.list(1,0,ManejadorMenu.ORDER_ID,"INI");
			
			out.println("<ul id=\"accordion\">");
			out.println("<li>");
			// item 1
			out.println("<div>Administrador</div>");
			out.println("<ul>");
			out.println("<li><a href=''>Opcion 1</a>");
			out.println("<li><a href=''>Opcion 1</a>");
			out.println("</ul>");
			out.println("</li>");
			
			
			// item 2
						out.println("<div>Menu</div>");
						out.println("<ul>");
						out.println("<li><a href=''>Opcion 1</a>");
						out.println("<li><a href=''>Opcion 1</a>");
						out.println("</ul>");
						out.println("</li>");
						
			/*for (Menu m : menuP)
			{
				out.println("<li><div>"+m.getTitulo()+"</div>");
				//out.println("<a href=\""+m.getUrl()+"\">"+m.getTitulo()+"</a>");
				ArrayList<Menu> menuH = MN.list(Integer.parseInt(m.getMenuid()), ManejadorMenu.ORDER_ID);
				out.println("<ul>");
				for(Menu me: menuH)
				{
					out.println("<li><a href=\""+me.getUrl()+"\">"+me.getTitulo()+"</a></li>");
				}
				out.println("</ul>");
				out.println("</li>");
			}*/
			out.println("</ul>");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
    }
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		ProcesarPeticion(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		ProcesarPeticion(request, response);
	}

}
